package top.drinyeit.weilong.presenter;

import android.support.v7.widget.LinearLayoutManager;

import java.util.List;
import java.util.Map;

import top.drinyeit.weilong.activity.StoreActivity;
import top.drinyeit.weilong.adapter.StoreHomeAdapter;
import top.drinyeit.weilong.fragment.StoreHomeFragment;
import top.drinyeit.weilong.model.StoreHomeBean;
import top.drinyeit.weilong.retrofit.HttpResponse;
import top.drinyeit.weilong.retrofit.RetrofitMethod;
import top.drinyeit.weilong.retrofit.subscriber.CommonSubscriber;
import top.drinyeit.weilong.retrofit.subscriber.SubscriberListener;

import static top.drinyeit.weilong.app.Constant.COMMON_SUCCESS;

/**
 * Created by wu on 2018/1/10.
 */

public class StoreHomePresenter {
    private StoreHomeFragment fragment;
    private StoreHomeAdapter storeHomeAdapter;
    public StoreHomePresenter(StoreHomeFragment fragment){
        this.fragment = fragment;
    }
    //获取店铺详情
    public void getStoreHome(Map<String,Object> map) {
        fragment.showLoading();
        CommonSubscriber<HttpResponse<List<StoreHomeBean>>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<List<StoreHomeBean>>>() {
            @Override
            public void onError(String e, int code) {
                fragment.dismissLoading();
                fragment.showToastMsg("错误，请重新操作！");
            }

            @Override
            public void onNext(HttpResponse<List<StoreHomeBean>> o) {
                fragment.dismissLoading();
                if (o.getCode() == COMMON_SUCCESS) {
                    storeHomeAdapter = new StoreHomeAdapter(o.getData());
                    fragment.mRecyclerView.setLayoutManager(new LinearLayoutManager(fragment.getActivity()));
                    fragment.mRecyclerView.setAdapter(storeHomeAdapter);
                }else {
//                    fragment.showToastMsg(o.getMessage());
                }
            }
        });
        RetrofitMethod.getInstance().getStoreHome(subscriber, map);
    }
}
